const app = getApp()
const s = new app.Shuju();
Page({
  data: {
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    canIUseGetUserProfile: false,
    canIUseOpenData: wx.canIUse('open-data.type.userAvatarUrl') && wx.canIUse('open-data.type.userNickName') // 如需尝试获取用户信息可改为false
  },
  // 事件处理函数
  bindViewTap() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad() {
    //获取openid
    this._openid(function (wxid) {
      console.log(wxid);
      wx.setStorageSync('wxid', wxid);
    });
  },

  _openid: function (call) {
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var appid = ''; //填写微信小程序appid 
        var secret = ''; //填写微信小程序secret 
        //调用request请求api转换登录凭证 
        wx.request({
          url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&grant_type=authorization_code&js_code=' + res.code,
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            const openid = res.data.openid;
            call(openid);
            console.log(res)

          },
          fail: function (e) {
            console.log(e)
          }
        })
      }
    })
  },

  _Nextstep: function () {
    //这里做个检查，就是这个用户到底有没有登录过
    let wxid = wx.getStorageSync('wxid');
    s.se('wxid', wxid);
    s.sb('user');
    s.se('tj', 'wxid');
    s.g('c/common_detail', {
      s: (d) => {
        if (d.code == 200) {
          wx.setStorageSync('id', d.data.id);
          wx.navigateBack({
            delta: 0,
          })
        } else {
          wx.navigateTo({
            url: 'wxinfo',
          })
        }
      }
    })

  },



})